//
//  FHStatusTableViewCell.swift
//  WeiBo
//
//  Created by qfh on 16/7/13.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit

import SnapKit

class FHStatusTableViewCell: UITableViewCell {
    
    
    //  记录toolbar顶部约束
    var toolbarTopConstraint: Constraint?
    
    //原创微博
    private lazy var statusOrigialView: FHStatusOriginalView = FHStatusOriginalView()
    // 底部信息
    private lazy var statusToolsBar: FHStatusToolsBar = FHStatusToolsBar()
    // 转发信息
    private lazy var statusRetweet: FHStatusRetweetView = FHStatusRetweetView()
    
    
    // 传递数据 viewModel
    var statusViewModel: FHStatusViewModel?  {
    
    
        didSet {
        
            statusOrigialView.statusViewMoel = statusViewModel
            
            statusToolsBar.statusView = statusViewModel
            
            self.toolbarTopConstraint?.uninstall()
            
            
            //  判断转发微博
            if statusViewModel?.status?.retweeted_status != nil {
            
            
                
                
                statusRetweet.statusViewMoel = statusViewModel
                
                statusRetweet.hidden = false
                
                statusToolsBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.toolbarTopConstraint = make.top.equalTo(statusRetweet.snp_bottom).constraint
                    
                    
                })
                
            
            
            }else {
            
                statusRetweet.hidden = true
                
                statusToolsBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                    
                    self.toolbarTopConstraint = make.top.equalTo(statusOrigialView.snp_bottom).constraint
                })
            }
            
            
            
            
            
        }
    }
    
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUi()
    }

    
    /**
     更新ui
     */
    func setupUi() {
    
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        contentView.addSubview(statusOrigialView)
        
        contentView.addSubview(statusToolsBar)
        
        contentView.addSubview(statusRetweet)
        
        
        statusOrigialView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(contentView).offset(5)
            
            make.trailing.equalTo(contentView)
            
            make.top.equalTo(contentView)

            
        }
        
        statusRetweet.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(statusOrigialView.snp_bottom)
            
            make.leading.equalTo(contentView)
            
            make.trailing.equalTo(contentView)

            
        }
        
        
        statusToolsBar.snp_makeConstraints { (make) -> Void in
            
            
            self.toolbarTopConstraint = make.top.equalTo(statusRetweet.snp_bottom).constraint
            
            make.leading.equalTo(contentView)
            
            make.trailing.equalTo(contentView)
            
            make.height.equalTo(35)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(statusToolsBar)
            
            make.leading.equalTo(self)
            
            make.trailing.equalTo(self)
            
            make.top.equalTo(self)
        }
    
    }
    
    
    
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


}
